package com.choosefine.springboot.devops.base.test;

import com.choosefine.springboot.devops.base.config.JsonUtil;
import com.choosefine.springboot.devops.base.config.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/30 0030.
 */
@RestController
@RequestMapping("/test")
public class test {
    String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    @Autowired
    private RestTemplate restTemplate;
    Logger log= LoggerFactory.getLogger("test");
    @GetMapping()
    public String testRequest(){
//        org.apache.http
        String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ TokenUtil.getToken();
        Map<String,Object> map=new HashMap<String, Object>();
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        Map<String,Object> button=new HashMap<String, Object>();
        button.put("type","click");
        button.put("name","hl");
        button.put("key","v1001");
        Map subBtton=new HashMap();
        subBtton.put("type","click");
        subBtton.put("name","xxxx");
        subBtton.put("key","ttttttt");
        button.put("sub_button",subBtton);
        list.add(button);
        map.put("button",list);
        String s= JsonUtil.mapToJson(map);
        System.out.println(map);
        restTemplate=new RestTemplate();
        MultiValueMap<String, Object> dataMap = new LinkedMultiValueMap();
        MultiValueMap<String, String> header = new LinkedMultiValueMap();
        dataMap.setAll(map);
//        header.set("Content-Type","application/x-www-form-urlencoded");
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(dataMap, header);
        ResponseEntity<String> exchan = restTemplate.exchange(url, HttpMethod.POST, requestEntity,new ParameterizedTypeReference<String>(){},"");
        String returnMsg=exchan.getBody();
        System.out.println(returnMsg);
        return null;
    }

   /* public static void main(String[] args) {
        test t=new test();
        t.testRequest();
    }*/
}
